L04 Maps

Data Visualization (STAT 302)

Author

DAVIS JOHNSON

Load Packages and Datasets

We’ll be using the US_income.rda dataset which should be placed in the /data subdirectory in our data_vis_labs project. You’ll also be downloading your own data to build maps.

Code
# Load package(s)
library(tidyverse)
library(tigris)
library(statebins)

# Load dataset(s)
load("data/US_income.rda")

Exercise 1

Plot 1

Make a county map of a US state using geom_polygon(). Maybe use your home state or a favorite state. Please do NOT use the state in the ggplot2 book example.

Optional: Consider adding major cities (or your home town).

Hints:

  • See section 6.1 in our book.
  • Void theme
Solution
Code
#get data
fl_counties <- map_data("county", "florida") %>% 
  select(lon = long, lat, group, id =subregion)

# city_data
fl_city_data <- tibble(
  city_name = c("Gainesville"),
  lat = c(29.651634),
  lon = c(-82.324829) 
  
)

# make map
ggplot(fl_counties, aes(lon, lat)) +
  geom_polygon(aes(group = group),fill = "white", color = "grey50") +
  ggstar::geom_star(data = fl_city_data, size = 3, starshap = 1,fill = "orange") +
  geom_text(data = fl_city_data, aes(label = city_name), vjust = -1, fontface = "bold", size = 5) +
  coord_quickmap() +
  theme_void() +
  ggtitle("Florida")

Plot 2

Now use geom_sf() instead. You’ll need to download data for this. You can use either the tigris (github page) or geodata packages. Either tigriscounties() with cb = TRUE or geodata’s gadm() could be useful.

Solution
Code
# Load South Dakota county boundaries
invisible(sd_counties <- counties(state = "SD", cb = TRUE))

# Plot county boundaries
ggplot(sd_counties) +
  geom_sf(fill = "white") +
  coord_sf() +
  ggtitle("South Dakota") +
  theme_void()

Exercise 2

Using the US_income dataset, recreate the following graphics as precisely as possible.

Code
# Setting income levels
US_income <- mutate(
  US_income,
  income_bins = cut(
    ifelse(is.na(median_income), 25000, median_income),
    breaks = c(0, 40000, 50000, 60000, 70000, 80000),
    labels = c("< $40k", "$40k to $50k", 
               "$50k to $60k", "$60k to $70k", "> $70k"),
    right = FALSE
  )
)

Plot 1

Hints:

  • geom_sf() — boundary color is "grey80" and size is 0.2
  • viridis package (discrete = TRUE in scale_* function)
  • Void theme
Solution
Code
#plot map
ggplot(US_income) +
  geom_sf(aes(geometry = geometry, fill = income_bins), color = "grey80", size = 0.2) +
  # create legend/scale
  viridis::scale_fill_viridis(discrete = TRUE, name = "Median\nIncome") +
  coord_sf() +
  theme_void()

Plot 2

Hints:

  • statebins::geom_statebins()
  • viridis package (discrete = TRUE in scale_* function)
  • Statebins theme
Solution
Code
ggplot(US_income, aes(state = name, fill = income_bins)) +
  statebins::geom_statebins() +
  viridis::scale_fill_viridis(discrete = TRUE, name = "Median\nIncome") +
  theme_statebins()

Exercise 3

Pick any city or foreign country to build a map for. You can dress it up or make it as basic as you want. Also welcome to try building a graphic like that depicted at the end of section 6.5 — use a different region though.

Solution
Code
# map of France
france_map <- map_data("france", region = ".", exact = FALSE)

#creating French capitals data frame
french_capitals <- tibble::tribble( 
  ~city,           ~lat,     ~long,
  "Paris",      48.8534, 2.3488,  
  "Marseille",  43.2965, 5.3698, 
  "Lyon",       45.7640, 4.8357, 
  "Toulouse",   43.6045, 1.4442, 
  "Nice",       43.7102, 7.2618, 
  "Nantes",     47.2184, -1.5536, 
  "Strasbourg", 48.5846, 7.7433, 
  "Montpellier",43.6108, 3.8767, 
  "Bordeaux",   44.8378, -0.5792,
  "Lille",      50.6293, 3.0573,
  "Rennes",     48.1173, -1.6743,
  "Reims",      49.2583, 4.0317,
  "Le Havre",   49.4938, 0.1016,
  "Saint-Etienne", 45.4397, 4.3872,
  "Toulon",     43.1242, 5.9306
)


#making plot
ggplot() +
  geom_polygon(data = france_map, aes(x = long, y = lat, group = group), 
               fill = "white", color = "black") +
  geom_point(data = french_capitals, aes(x = long, y = lat), color = "red", size = 2.5) +
  coord_sf() +
  theme_void()

Challenge(s)

Optional

Using the tidycensus package and few others, try to create a map like below using these directions. Try using a different geographical area and a different variable from the ACS.